﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Editor
{
    public class TileList
    {
        private List<CompleteTile> _tiles = new List<CompleteTile>();
        private Graphics _renderSurface;
        private Graphics _imageRender;
        private Bitmap _tileImages;
        private EditorCursor _cursor;
        private Board _gameBoard;
        private Size _cellSize;
        private Point _currentTileLocation;
        private Point _tileScroll = new Point(0, 0);
        private CompleteTile _currentTile = new CompleteTile();
        private int _tileOffsetY = 0;
        int _gridX = 0;
        int _gridY = 0;
        private int _location
        {
            get { return (_currentTileLocation.Y * 4 + _currentTileLocation.X); }
        }
        public delegate void ReturnTile(iTile tile);
        public event ReturnTile EditTile;
        public delegate void NullDelegate();
        public event NullDelegate CreateTile;
        public TileList(ref Graphics renderSurface, Size cellSize, ref Board gameBoard)
        {
            _cellSize = cellSize;
            _renderSurface = renderSurface;
            _gameBoard = gameBoard;
            _tileImages = new Bitmap((int)renderSurface.VisibleClipBounds.Width, _cellSize.Height * 30);
            _imageRender = Graphics.FromImage(_tileImages);
            _cursor = new EditorCursor(ref _renderSurface, ref _tileImages);
            InitGraphics();
        }
        private void InitGraphics()
        {
           _imageRender.Clear(Color.Black);
        }
        public void UpdateMousePosition(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            _gridX = (int)(e.Location.X / _cellSize.Width);
            _gridY = (int)(e.Location.Y / _cellSize.Height);
            UpdateCursor();
        }

        public void TileClicked(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            _currentTileLocation.Y = _tileOffsetY + _gridY;
            _currentTileLocation.X = _gridX;
        }

        public void ScrollTileList(object sender, System.Windows.Forms.ScrollEventArgs e)
        {
            _tileScroll.Y = e.NewValue;
            Point drawPoint = new Point(0, (_tileScroll.Y) * 32);
            _cursor.backgroundLocation = drawPoint;
            _cursor.DrawRectangle1();
        }
        public void TileDoubleClicked(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            _currentTileLocation.Y = _tileOffsetY + _gridY;
            _currentTileLocation.X = _gridX;
            try
            {
                EditTile((iTile)_tiles[_location]);
            }
            catch 
            {
                CreateTile();
            }            
        }
        
        private void UpdateCursor()
        {
            _cursor.UpdatePosition(_gridX, _gridY);
        }
        public void UpdateTile()
        {
            int row = _location / 4;
            int column = _location % 4;
            _imageRender.DrawImage(_tiles[_location].tileImage, new Rectangle(column * 32, row * 32, 32, 32), new Rectangle(0, 0, 32, 32), GraphicsUnit.Pixel);
            _cursor.DrawRectangle1();
            _gameBoard.ReDrawBoard();
        }
        public void AddTile(CompleteTile tile)
        {
            int Location  = _tiles.Count();
            _tiles.Add(tile);
            int row = Location / 4;
            int column = Location % 4;
            _imageRender.DrawImage(tile.tileImage, new Rectangle(column * 32, row * 32, 32, 32), new Rectangle(0, 0, 32, 32), GraphicsUnit.Pixel);
            _cursor.DrawRectangle1();
        }
        public void AssignBoardTile(int boardRow, int boardColumn)
        {
            try
            {
                _gameBoard.ChangeTile(boardRow, boardColumn, (iTile)_tiles[_location]);
            }
            catch { }
        }        
    }
}
